/**
 * @date 9:10:10 AM
 * EducationHomeWorkQuestionDaoImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.bean.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qs.education.bean.dao.IEducationHomeWorkQuestionDao;
import com.qs.education.bean.entity.EducationHomeWork;
import com.qs.education.bean.entity.EducationHomeWorkQuestion;
import com.qs.education.bean.entity.EducationQuestion;

/**
 * @class EducationHomeWorkQuestionDaoImpl	
 * @author Warren
 * @description
 * @date Nov 22, 2018 
 */
@Repository
public class EducationHomeWorkQuestionDaoImpl extends BaseDaoImpl<EducationHomeWorkQuestion> implements IEducationHomeWorkQuestionDao{

	/**
	 * @author Warren
	 * @param homework
	 * @return ResponseListBean<EducationHomeWorkQuestion>
	 * @description 根据作业id获得作业的所有问题
	 * @date Nov 22, 2018
	 */
	@Override
	public List<EducationHomeWorkQuestion> getALLQuestionByHomeWorkId(EducationHomeWork homework) {
		String jql = "from EducationHomeWorkQuestion eq where eq.homework = :homework";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("homework", homework);
		List<EducationHomeWorkQuestion> list = query(jql, EducationHomeWorkQuestion.class, params);
		if(list!=null && !list.isEmpty()) {
			return list;
		}
		return null;
	}

	/**
	 * @author Warren
	 * @param educationQuestion
	 * @param educationHomeWork
	 * @return Long
	 * @description 根据homework和question获得主键id
	 * @date Nov 22, 2018
	 */
	@Override
	public Long getIdByHomeWorkAndQuestion(EducationQuestion educationQuestion,EducationHomeWork educationHomeWork) {
		String jql = "Select id from EducationHomeWorkQuestion eq where eq.homework = :homework and eq.question = :question";
		Query query = em.createQuery(jql);
		query.setParameter("homework", educationHomeWork);
		query.setParameter("question", educationQuestion);
		return (Long)query.getSingleResult();
	}

	/**
	 * @author Warren
	 * @param educationQuestion
	 * @param educationHomeWork
	 * @return Long
	 * @description 根据homework和question获得EducationHomeWorkQuestion
	 * @date Nov 22, 2018
	 */
	@Override
	public EducationHomeWorkQuestion findByHomeWorkAndQuestion(EducationQuestion educationQuestion,
			EducationHomeWork educationHomeWork) {
		String jql = "from EducationHomeWorkQuestion eq where eq.homework = :homework and eq.question = :question";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("homework", educationHomeWork);
		params.put("question", educationQuestion);
		List<EducationHomeWorkQuestion> result = query(jql, EducationHomeWorkQuestion.class, params);
		if(result!=null && !result.isEmpty()) {
			return result.get(0);
		}
		return null;
	}
	
}
